import { _decorator, dragonBones, JsonAsset } from "cc";
import { app } from "db://app-game/core/app";
import AnimatorDragonBones from "db://app-game/libs/animator/AnimatorDragonBones";
import { AnimatorStateLogic } from "db://app-game/libs/animator/core/AnimatorStateLogic";
import { Role } from "../Role";
import { RoleLowerBodyAnimatorEnum } from "../Role.type";
import { AnimationEventHandler } from "./animator/AnimationEventHandler";
import { RoleLowerStateIdle } from "./animator/lower-body/RoleLowerStateIdle";
import { RoleLowerStateWalkx } from "./animator/lower-body/RoleLowerStateWalkx";

const { ccclass, property, requireComponent, disallowMultiple } = _decorator;
/**
 * 角色SPINE动画控制
 *
 * 实现功能
 * 1、控制动作变化
 * 2、控制武器变化
 * 3、控制脸的朝向
 */
@ccclass("RoleViewLowerBodyAnimator")
@disallowMultiple
@requireComponent(dragonBones.ArmatureDisplay)
export class RoleViewLowerBodyAnimator extends AnimatorDragonBones {
  /** 角色对象 */
  role: Role = null!;

  protected async start(): Promise<void> {
    // 1.获取状态机
    const animatorGraphPath =
      this.role.RoleModel.data.anim.lowerBody.animatorGraphPath;
    const config = await app.res.loadAsync(animatorGraphPath, JsonAsset);
    this.AssetRawUrl = config;

    // 2.初始化状态机
    super.start();

    // 动画状态机
    let anim = new AnimationEventHandler(this.playAnimation);
    let asl: Map<string, AnimatorStateLogic> = new Map();
    asl.set(
      RoleLowerBodyAnimatorEnum.Idle,
      new RoleLowerStateIdle(this.role, anim)
    );
    asl.set(
      RoleLowerBodyAnimatorEnum.Walk,
      new RoleLowerStateWalkx(this.role, anim)
    );

    this.initArgs(asl, anim);
  }

  /** 动态变化回调 */
  onStateChangeCall(fromState: string, toState: string) {
    console.log(fromState, toState);
  }
}
